<?php

namespace Home\Controller;

use Think\Controller;
use Home\Model\GameModel;
use Home\Model\UserModel;
use Home\Model\LibaoModel;

class LibaoController extends Controller {

    private $gameModel;
    private $libaoModel;

    public function __construct() {
        parent::__construct();
        $userModel = new UserModel;
        if ($userModel->check_login() < 0) {
            $this->assign('msgTitle', '登录');
            $this->assign('message', '你还未登录，你先登录！');
            $this->assign('waitSecond', 5);
            $this->assign('jumpUrl', U('user/login'));
            $this->error();
        }
        $this->gameModel = new GameModel();
        $this->libaoModel = new LibaoModel();
    }

    public function index() {
        $gamelist = $this->gameModel->gameAll();
        $this->assign('gamelist', $gamelist);
        $this->assign('libaoCss', 'on');
        $this->display();
    }

    public function game() {
        $game_id = intval($_GET['game_id']);
        $game = $this->libaoModel->getLibaoByid($game_id);

        //根据game_id获得服务器列表
        $server = $this->gameModel->serverList(0, $game_id);
        //var_dump($server);
        $this->assign('server', $server);
        $this->assign('game', $game);
        $this->assign('libaoCss', 'on');
        $this->display();
    }

    public function libao() {
        session('[start]');
        $game_id = intval($_POST['game_id']);
        $enname = I('post.enname');
        $tag = I('post.tag');
        $general = I('post.general');
        $port = I('post.port');
        $area_num=intval($_POST['area_num']);
        $serverModel=M('game_server_list');
        $server = $serverModel->where("game_id='%d' and area_num='%d'",array($game_id,$area_num))->find();
        /**
         * 如果$general为t说明是通服为f说明不是通服
         * 如果$port为t说明是接口为f说明不是接口
         */
        if ($port == 't') {
            $class = ucfirst($enname);
            require_cache(APP_PATH . 'Home/Gameapi/' . $class . '.class.php');
            $card=$class::get_libao(session('userinfo'),$server);
            $this->ajaxReturn($card);
        }
    }

}
